package cn.zhz.privacy.sensitive.handler;

import cn.hutool.core.util.StrUtil;
import cn.zhz.privacy.sensitive.SensitiveType;
import cn.zhz.privacy.sensitive.SensitiveTypeHandler;

/**
 * 邮件信息脱敏处理类
 * 邮箱前缀仅显示第一个字母，前缀其他隐藏，用星号代替，@及后面的地址显示
 * 例子:g**@163.com
 * @author ;
 */
public class EmailSensitiveHandler implements SensitiveTypeHandler {
    @Override
    public SensitiveType getSensitiveType() {
        return SensitiveType.EMAIL;
    }

    @Override
    public String handle(Object src) {
        if(src==null){
            return null;
        }
        String email = src.toString();
        int index = StrUtil.indexOf(email, '@');
        if (index <= 1) {
            return email;
        } else {
            return StrUtil.padAfter(StrUtil.subPre(email, 1), index, "*")
                    .concat(StrUtil.subSuf(email, index));
        }
    }
}
